<?php

App::uses('AppController', 'Controller');

/**
 * Menus Controller
 *
 * @property Menu $Menu
 * @property PaginatorComponent $Paginator
 */
class MenusController extends AppController {
    /**
     * Components
     *
     * @var array
     */
    //public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function index($role=null) {
        $this->Menu->locale =  $this->Session->read('Language.code');
        if ($role == 0){
            $condition = array('Menu.role'=>0);   
        }if ($role == 1){
            $condition = array('Menu.role'=>1);
        }else if($role == 2){
            $condition = array('Menu.role'=>2);
        }
        $this->paginate = array('order' => array('Menu.role'=>'asc','Menu.lft' => 'asc' ), 'limit'=>$this->Menu->find('count'), 'conditions'=>$condition);
        $this->Menu->recursive = 0;
        $this->set('menus', $this->paginate());

    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Menu->locale =  $this->Session->read('Language.code');
        if (!$this->Menu->exists($id)) {
            throw new NotFoundException('Invalid menu','default',array('class'=>'alert alert-danger'));
        }
        $options = array('conditions' => array('Menu.' . $this->Menu->primaryKey => $id));
        $this->set('menu', $this->Menu->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->Menu->locale =  $this->Session->read('Language.code');
        $this->loadModel('Language');
        $menu = $this->Menu->find('list');
        $this->set(compact('menu'));
        $this->Menu->content->locale = $this->Session->read('Language.code');
        $content = $this->Menu->content->find('list');
        $this->set(compact('content'));
        $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
        $this->set(compact('language'));
        if ($this->request->is('post')) {
            $this->Menu->create();
            $this->Menu->locale = $this->request->data['Menu']['Language'];
            // set parent_id = value if other than no menu content is chosen
            if ($this->request->data['Menu']['parent_id'] == 0) {
                // set parent_id = null value if no menu content is chosen
                $this->request->data['Menu']['parent_id'] = null;
            }
            //set slug for content
            if ($this->request->data['Menu']['c_id'] != 0) {
                $condition = array('Content.id' => $this->request->data['Menu']['c_id']);
                $content = $this->Menu->content->find('first', array('conditions' => $condition));
                $this->request->data['Menu']['path'] = '/contents/view/' . $content['content']['path'];
            }
            if ($this->Menu->save($this->request->data)) {
                $this->Session->setFlash('The new menu has been saved','default',array('class'=>'alert alert-info'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The new menu could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Menu->locale =  $this->Session->read('Language.code');
        $this->loadModel('Language');
        $menu = $this->Menu->find('list');
        $this->set(compact('menu'));
        $this->Menu->content->locale = $this->Session->read('Language.code');
        $content = $this->Menu->content->find('list');
        $this->set(compact('content'));
        $language = $this->Language->find('list', array('fields' => array('Language.code', 'Language.name')));
        $this->set(compact('language'));
        if (!$this->Menu->exists($id)) {
            throw new NotFoundException('Invalid menu','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->Menu->locale = $this->request->data['Menu']['Language'];
            // set parent_id = value if other than no menu content is chosen
            if ($this->request->data['Menu']['parent_id'] == 0) {
                // set parent_id = null value if no menu content is chosen
                $this->request->data['Menu']['parent_id'] = null;
            }
            //set slug for content
            if ($this->request->data['Menu']['c_id'] != 0) {
                $condition = array('Content.id' => $this->request->data['Menu']['c_id']);
                $content = $this->Menu->content->find('first', array('conditions' => $condition));
                $this->request->data['Menu']['path'] = '/contents/view/' . $content['content']['path'];
            }
            if ($this->Menu->save($this->request->data)) {
                $this->Session->setFlash('The menu has been successfully edited','default',array('class'=>'alert alert-info'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('The menu could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
            }
        } else {
            $options = array('conditions' => array('Menu.' . $this->Menu->primaryKey => $id));
            $this->request->data = $this->Menu->find('first', $options);
        }

    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Menu->id = $id;
        if (!$this->Menu->exists()) {
            throw new NotFoundException('Invalid menu','default',array('class'=>'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Menu->delete()) {
            $this->Session->setFlash('Menu deleted','default',array('class'=>'alert alert-info'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Menu was not deleted','default',array('class'=>'alert alert-danger'));
        return $this->redirect(array('action' => 'index'));
    }

    public function movedown($id = null, $delta = 1) {
        $this->Menu->id = $id;
        if (!$this->Menu->exists()) {
            throw new NotFoundException('Invalid Menu','default',array('class'=>'alert alert-danger'));
        }
        $this->Menu->moveDown($this->Menu->id, abs($delta));
        return $this->redirect(array('action' => 'index'));
    }

    public function moveup($id = null, $delta = 1) {
        $this->Menu->id = $id;
        if (!$this->Menu->exists()) {
            throw new NotFoundException('Invalid Menu','default',array('class'=>'alert alert-danger'));
        }
        $this->Menu->moveUp($this->Menu->id, abs($delta));
        return $this->redirect(array('action' => 'index'));
    }

    public function menu() {
        $this->Menu->locale =  $this->Session->read('Language.code');
      if ($this->Session->read('Auth.User.role_id') == 1) {
            $condition = array('Menu.role'=>2);
            $menu = $this->Menu->find('all', array('order' => array('lft' => 'ASC'), 'conditions'=>$condition));
        } else if ($this->Session->read('Auth.User.role_id') == 2 || $this->Session->read('Auth.User.role_id') == 3 || $this->Session->read('Auth.User.role_id') == 4) {
            $condition = array('Menu.role'=>1);
            $menu = $this->Menu->find('all', array('order' => array('lft' => 'ASC'), 'conditions'=>$condition));
        } else {
            $condition = array('Menu.role'=>0);
            $menu = $this->Menu->find('all', array('order' => array('lft' => 'ASC'), 'conditions'=>$condition));
        }


        if (isset($this->params['requested'])) {
            return $menu;
        }
        $this->set(compact('menu', $menu));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('menu');
    }

}
